﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Seller
{
    public partial class wfDjackVehicleInfo : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void getVehicleInfo()
        {
            string vehicleId = this.GetParamHtValue("vehicleId");
            if (string.IsNullOrEmpty(vehicleId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                DataSet ds = vehicleInfo.GetVehicleByIds(vehicleId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                DataColumn dc = new DataColumn("VEHICLE_STATUS_NAME");
                ds.Tables[0].Columns.Add(dc);
                ds.Tables[0].Rows[0]["VEHICLE_STATUS_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleStatusEnum), ds.Tables[0].Rows[0]["VEHICLE_STATUS"]);

                DataColumn dc1 = new DataColumn("VEHICLE_BRAND_NAME");
                ds.Tables[0].Columns.Add(dc1);
                ds.Tables[0].Rows[0]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), Convert.ToInt32(ds.Tables[0].Rows[0]["VEHICLE_BRAND"].ToString()));
                DataColumn dc2 = new DataColumn("VEHICLE_SERVICE_TYPE_NAME");
                ds.Tables[0].Columns.Add(dc2);
                ds.Tables[0].Rows[0]["VEHICLE_SERVICE_TYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.ServiceTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[0]["VEHICLE_SERVICE_TYPE"].ToString()));

                DataColumn dc3 = new DataColumn("VEHICLE_TYPE_NAME");
                ds.Tables[0].Columns.Add(dc3);
                ds.Tables[0].Rows[0]["VEHICLE_TYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[0]["VEHICLE_TYPE"].ToString()));

                DataColumn dc4 = new DataColumn("VEHICLE_COLOR_NAME");
                ds.Tables[0].Columns.Add(dc4);
                ds.Tables[0].Rows[0]["VEHICLE_COLOR_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleColorEnum), Convert.ToInt32(ds.Tables[0].Rows[0]["VEHICLE_COLOR"].ToString()));
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"getVehicleInfo(" + retJson + ")");
            }
        }

        #region 初始化地区下拉列表

        private void showPlace1()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string country = this.GetParamHtValue("country");
            string countryName = this.GetParamHtValue("countryName");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(country))
                {
                    country = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    countryName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('country_select',80,'6'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + country + "','" + countryName + "');");
                showPlace2(country);
            }

        }
        private StringBuilder[] packageCityArray(DataSet ds)
        {
            StringBuilder[] cityArray = new StringBuilder[2];
            StringBuilder listValue = new StringBuilder();
            StringBuilder listOption = new StringBuilder();
            listValue.Append(@"[");
            listOption.Append(@"[");
            bool isFirst = true;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    listValue.Append(",");
                    listOption.Append(",");
                }
                listValue.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"] + @"""");
                listOption.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"] + @"""");
            }
            listValue.Append(@"]");
            listOption.Append(@"]");
            cityArray[0] = listValue;
            cityArray[1] = listOption;
            return cityArray;
        }
        private void showPlace2(string strPlace1Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string city = this.GetParamHtValue("city");
                string cityName = this.GetParamHtValue("cityName");
                DataSet ds = cbcc.SelectCountryByParentId(strPlace1Id);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(city))
                {
                    city = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    cityName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('city_select',80,'7'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + city + "','" + cityName + "');");
                showPlace3(city);
            }
        }
        private void showPlace3(string strPlace2Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string destion = this.GetParamHtValue("destion");
                string destionName = this.GetParamHtValue("destionName");
                DataSet ds = cbcc.SelectCountryByParentId(strPlace2Id);
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(destion))
                {
                    destion = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    destionName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('destion_select',80,'8'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + destion + "','" + destionName + "');");
            }
        }
        #endregion

        private void showVehicleType()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                string vehicleType = this.GetParamHtValue("vehicleType");
                string vehicleTypeName = this.GetParamHtValue("vehicleTypeName");
                DataTable dt = vehicleInfo.GetVehicleType();
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                int listNum = dt.Rows.Count;
                listValue.Append(@"[");
                listOption.Append(@"[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        listValue.Append(",");
                        listOption.Append(",");
                    }
                    listValue.Append(@"""" + dt.Rows[i]["enum_Value"] + @"""");
                    listOption.Append(@"""" + dt.Rows[i]["enum_Name"] + @"""");
                }
                listValue.Append(@"]");
                listOption.Append(@"]");
                if (string.IsNullOrEmpty(vehicleType))
                {
                    vehicleType = dt.Rows[0]["enum_Value"].ToString();
                    vehicleTypeName = dt.Rows[0]["enum_Name"].ToString();
                }
                this.ExecJScript(@"setArea('carType',234,'5'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + vehicleType + "','" + vehicleTypeName + "');");
            }
        }
        private void showVehicleBrand()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                string vehicleBrand = this.GetParamHtValue("vehicleBrand");
                string vehicleBrandName = this.GetParamHtValue("vehicleBrandName");
                DataTable dt = vehicleInfo.GetVehicleBrand();
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                int listNum = dt.Rows.Count;
                listValue.Append(@"[");
                listOption.Append(@"[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        listValue.Append(",");
                        listOption.Append(",");
                    }
                    listValue.Append(@"""" + dt.Rows[i]["enum_Value"] + @"""");
                    listOption.Append(@"""" + dt.Rows[i]["enum_Name"] + @"""");
                }
                listValue.Append(@"]");
                listOption.Append(@"]");
                if (string.IsNullOrEmpty(vehicleBrand))
                {
                    vehicleBrand = dt.Rows[0]["enum_Value"].ToString();
                    vehicleBrandName = dt.Rows[0]["enum_Name"].ToString();
                }
                this.ExecJScript(@"setArea('carBrand',234,'4'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + vehicleBrand + "','" + vehicleBrandName + "');");
            }
        }
        private void showVehicleColor()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                string vehicleColor = this.GetParamHtValue("vehicleColor");
                string vehicleColorName = this.GetParamHtValue("vehicleColorName");
                DataTable dt = vehicleInfo.GetVehicleColor();
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                int listNum = dt.Rows.Count;
                listValue.Append(@"[");
                listOption.Append(@"[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        listValue.Append(",");
                        listOption.Append(",");
                    }
                    listValue.Append(@"""" + dt.Rows[i]["enum_Value"] + @"""");
                    listOption.Append(@"""" + dt.Rows[i]["enum_Name"] + @"""");
                }
                listValue.Append(@"]");
                listOption.Append(@"]");
                if (string.IsNullOrEmpty(vehicleColor))
                {
                    vehicleColor = dt.Rows[0]["enum_Value"].ToString();
                    vehicleColorName = dt.Rows[0]["enum_Name"].ToString();
                }
                this.ExecJScript(@"setArea('carColor',234,'3'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + vehicleColor + "','" + vehicleColorName + "');");
            }
        }

        private void initplace()
        {
            showPlace1();
            showVehicleType();
            showVehicleBrand();
            showVehicleColor();
        }
        #region 地区下拉列表改变
        private void getArea2()
        {
            string parentsId = this.GetParamHtValue("parentsId");

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"updateSelet('city_select'," + retJson + ");");
                getArea3(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }
        }
        private void getArea3(string getArea3Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string isAll = this.GetParamHtValue("isAll");
                DataSet ds = cbcc.SelectCountryByParentId(getArea3Id);
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"updateSelet('destion_select'," + retJson + ");");
            }
        }
        #endregion

        private void saveVehicleInfo()
        {
            string djackId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djackId))
            {
                this.ExecJScript(@"window.location.href = ""/""");
                return;
            }
            string carName = this.GetParamHtValue("carName");
            if (string.IsNullOrEmpty(carName))
            {
                this.Alert("车辆名称不能为空");
                return;
            }
            string uetoTime = this.GetParamHtValue("uetoTime");
            if (string.IsNullOrEmpty(uetoTime))
            {
                this.Alert("购买时间不能为空");
                return;
            }
            string numberSeats = this.GetParamHtValue("numberSeats");
            if (string.IsNullOrEmpty(numberSeats))
            {
                this.Alert("座位数不能为空");
                return;
            }
            string carPrice = this.GetParamHtValue("carPrice");
            if (string.IsNullOrEmpty(carPrice))
            {
                this.Alert("车辆价格不能为空");
                return;
            }
            string servicesRange = this.GetParamHtValue("servicesRange");
            if (string.IsNullOrEmpty(servicesRange))
            {
                this.Alert("服务范围不能为空");
                return;
            }
            string carType = this.GetParamHtValue("carType");
            if (string.IsNullOrEmpty(carType))
            {
                this.Alert("车辆类型不能为空");
                return;
            }
            string carBrand = this.GetParamHtValue("carBrand");
            if (string.IsNullOrEmpty(carBrand))
            {
                this.Alert("车辆品牌不能为空");
                return;
            }
            string carColor = this.GetParamHtValue("carColor");
            if (string.IsNullOrEmpty(carColor))
            {
                this.Alert("车辆颜色不能为空");
                return;
            }
            string serviceType = this.GetParamHtValue("serviceType");
            if (string.IsNullOrEmpty(serviceType))
            {
                this.Alert("服务类型不能为空");
                return;
            }
            string carImg = this.GetParamHtValue("carImg");
            if (string.IsNullOrEmpty(carImg))
            {
                this.Alert("车辆图片不能为空");
                return;
            }
            string carIntroduction = this.GetParamHtValue("carIntroduction");
            if (string.IsNullOrEmpty(carIntroduction))
            {
                this.Alert("车辆简介不能为空");
                return;
            }
            string purchaseNotes = this.GetParamHtValue("purchaseNotes");
            if (string.IsNullOrEmpty(purchaseNotes))
            {
                this.Alert("购买须知不能为空");
                return;
            }
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destionId = this.GetParamHtValue("destionId");
            if (string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(destionId))
            {
                this.Alert("车辆所在地不能为空");
                return;
            }
            string vehicleId = this.GetParamHtValue("vehicleId");
            if (string.IsNullOrEmpty(vehicleId))
            {
                this.Alert("车辆编号不能为空");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
                {
                    //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                    carImg = oCBizSysFile.SaveImgToServer(""
                        , msBsc.SvRef.UserInfo.Operator
                        , "1"
                        , "VEH"
                        , ""
                        , carImg//文件来源虚拟路径
                        , ""//文件来源物理路径，虚拟和物理只有一个就行
                        , true);
                }
                string[] retArry = vehicleInfo.EditVehicleInfo(djackId,vehicleId, carName, uetoTime, numberSeats, carPrice, servicesRange, destionId, cityId, countryId, carType, carBrand, carColor, serviceType, carImg, carIntroduction, purchaseNotes);
                if (retArry[0] != "TRUE")
                {
                    this.Alert(retArry[1]);
                }
                else
                {
                    this.ExecJScript(@"alert(""修改成功"");window.location.reload(true);");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getVehicleInfo();
            }
            else if (this.strAction == "initCity")
            {
                showPlace1();
                showVehicleType();
                showVehicleColor();
                showVehicleBrand();
            }
            else if (this.strAction == "getArea2")
            {
                getArea2();
            }
            else if (this.strAction == "getArea3")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea3(parentsId);
            }
            else if (this.strAction=="saveVehicle")
            {
                saveVehicleInfo();
            }
        }
    }
}